<template>
  <div>
    <!-- 顶部滑动条 -->
    <div id="slider" class="mui-slider">
      <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
        <div class="mui-scroll">
          <a :class="['mui-control-item', item.cat_id == 0 ? 'mui-active' : '']" v-for="(item, index) in categories" :key="index" @tap="getPhotoes(item.cat_id)">
            {{ item.title }}
          </a>
        </div>
      </div>
    </div>
    <!-- 图片列表 -->
    <ul class="photo-list">
      <router-link v-for="(item, index) in photoes" :key="index" :to="'/photo/details/'+item._id" tag="li">
        <img class="photo" v-lazy="item.img_url+'?'+item._id">
        <div class="info">
          <h1 class="info-title">{{ item.title }}</h1>
          <div class="info-body">{{ item.description }}</div>
        </div>
      </router-link>
    </ul>
  </div>
</template>

<script>
// 1. 导入 mui 的js文件
import mui from "../../lib/mui/js/mui.min.js";
import { Toast } from "mint-ui";

export default {
  data() {
    return {
      categories: [], // 所有图片分类
      photoes: [] // 图片
    };
  },
  methods: {
    getAllCategories() {
      this.$http.get("getphotocat").then(result => {
        if (result.status === 200) this.categories = result.body;
        else Toast("图片列表加载失败");
      });
    },
    getPhotoes(cat_id) {
      this.$http.get("getphotoes?catid=" + cat_id).then(result => {
        if (result.status === 200) this.photoes = result.body;
        else Toast("图片加载失败");
      });
    }
  },
  created() {
    this.getAllCategories();
    this.getPhotoes(0);
  },
  mounted() {
    // 如果要操作元素了，最好在 mounted 生命周期钩子函数中进行，因为这时候的 DOM 元素是最新的
    // 2. 初始化滑动控件,当组件中的DOM结构被渲染好并挂载到页面中后，再初始化滑动控件，否则会出现首次进入该页面不能滑动的情况
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
    });
  }
};
</script>

<style lang="scss" scoped>
* {
  touch-action: pan-y; //禁止滑动时 Chrome 浏览器的警告
}
.photo-list {
  list-style: none;
  padding: 10px;
  padding-bottom: 0;
  margin: 0;
  li {
    margin-bottom: 10px;
    background-color: #cccccc;
    text-align: center;
    box-shadow: 0 0 9px #999;
    position: relative;
    img {
      width: 100%;
      vertical-align: middle; //去图片3px问题
    }
    img[lazy="loading"] {
      width: 40px;
      height: 300px;
      margin: auto;
    }
    .info {
      color: #fff;
      text-align: left;
      position: absolute;
      bottom: 0;
      background-color: rgba($color: #000000, $alpha: 0.4);
      max-height: 84px;
      .info-title {
        font-size: 14px;
      }
      .info-body {
        font-size: 13px;
      }
    }
  }
}
</style>

